﻿
SET QUOTED_IDENTIFIER ON
GO

SET ANSI_NULLS ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[fn_compare_int]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
    DROP FUNCTION [dbo].[fn_compare_int];
GO
CREATE FUNCTION [dbo].[fn_compare_int]
(
    @a int,
    @b int,
    @op nvarchar(10)
)
returns bit
as
begin
    if @op = '>'
    begin
        if @a > @b
            return 1;
        return 0;
    end

    if @op = '='
    begin
        if @a = @b
            return 1;
        return 0;
    end

    if @op = '<'
    begin
        if @a < @b
            return 1;
        return 0;
    end

    if @op = '>='
    begin
        if @a >= @b
            return 1;
        return 0;
    end

    if @op = '<='
    begin
        if @a <= @b
            return 1;
        return 0;
    end

    -- The last operator that we support must be '<>'
    if @a <> @b
        return 1;
    return 0;
end
GO